package com.it.smartoldb.controller;

import com.it.smartoldb.common.Result;
import com.it.smartoldb.entity.HealthRecord;
import com.it.smartoldb.service.HealthRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 健康记录控制器
 */
@RestController
@RequestMapping("/api/health-records")
@Tag(name = "健康记录管理", description = "健康记录的增删改查操作")
public class HealthRecordController {

    @Autowired
    private HealthRecordService healthRecordService;

    /**
     * 创建健康记录
     */
    @PostMapping
    @Operation(summary = "创建健康记录", description = "添加新的健康记录")
    public Result<HealthRecord> createHealthRecord(@RequestBody HealthRecord healthRecord) {
        try {
            HealthRecord created = healthRecordService.createHealthRecord(healthRecord);
            return Result.success(created);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 更新健康记录
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新健康记录", description = "根据ID更新健康记录")
    public Result<HealthRecord> updateHealthRecord(@PathVariable Long id, @RequestBody HealthRecord healthRecord) {
        try {
            healthRecord.setId(id);
            HealthRecord updated = healthRecordService.updateHealthRecord(healthRecord);
            return Result.success(updated);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 删除健康记录
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除健康记录", description = "根据ID删除健康记录")
    public Result<Void> deleteHealthRecord(@PathVariable Long id) {
        try {
            healthRecordService.deleteHealthRecord(id);
            return Result.success();
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 根据ID获取健康记录
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取健康记录详情", description = "根据ID获取健康记录详情")
    public Result<HealthRecord> getHealthRecord(@PathVariable Long id) {
        try {
            HealthRecord record = healthRecordService.findById(id);
            return Result.success(record);
        } catch (RuntimeException e) {
            return Result.error(404, e.getMessage());
        }
    }

    /**
     * 分页查询健康记录
     */
    @GetMapping
    @Operation(summary = "分页查询健康记录", description = "支持多条件搜索的分页查询")
    public Result<Page<HealthRecord>> getHealthRecords(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long elderlyId,
            @RequestParam(required = false) String recordType,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        Page<HealthRecord> records = healthRecordService.findWithFilters(page, size, elderlyId, recordType, startTime, endTime);
        return Result.success(records);
    }

    /**
     * 获取老人的健康记录（分页）
     */
    @GetMapping("/elderly/{elderlyId}")
    @Operation(summary = "获取老人健康记录", description = "获取指定老人的健康记录列表")
    public Result<Page<HealthRecord>> getHealthRecordsByElderlyIdPaged(
            @PathVariable Long elderlyId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        Page<HealthRecord> records = healthRecordService.findByElderlyId(elderlyId, page, size);
        return Result.success(records);
    }

    /**
     * 获取健康记录统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取健康记录统计", description = "获取健康记录的统计信息")
    public Result<Map<String, Object>> getHealthRecordStatistics() {
        Map<String, Object> statistics = healthRecordService.getStatistics();
        return Result.success(statistics);
    }
}